package com.xinghou.platform.dao.mange.impl;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;

import com.xinghou.platform.dao.bases.JdbcTemplate;
import com.xinghou.platform.dao.bases.database.CustomerContextHolder;
import com.xinghou.platform.dao.mange.VestAssignedDAO;
import com.xinghou.platform.model.main.VestAssigned;

/**
 * 
 * @author wlw
 * 
 */
@Repository
public class VestAssignedDAOImpl implements VestAssignedDAO {
	@Resource
	private JdbcTemplate jdbcTemplate;
	
	public List<VestAssigned> getvestAssigned(Integer vestId) throws DataAccessException {
		CustomerContextHolder.setCustomerType(CustomerContextHolder.DATA_SOURCE_READ);
		String sql="SELECT 	id,vest_id,true_id,fake_id,true_name,fake_name,TEXT,STATUS,msg_id FROM vestassigned WHERE vest_id=?";
		List<VestAssigned> vestAssignedlist = jdbcTemplate.query(sql, new Object[] {vestId}, new VestAssignedMapper());
		CustomerContextHolder.clearCustomerType();
		return vestAssignedlist;
	}
	
	

	private static final class VestAssignedMapper implements RowMapper<VestAssigned> {
		public VestAssigned mapRow(ResultSet rs, int rowNum) throws SQLException {
			VestAssigned vestAssigned = new VestAssigned();
			vestAssigned.setFakeId(rs.getInt("fake_id"));
			vestAssigned.setFakeName(rs.getString("fake_name"));
			vestAssigned.setId(rs.getInt("id"));
			vestAssigned.setTest(rs.getString("TEXT"));
			vestAssigned.setTrueId(rs.getInt("true_id"));
			vestAssigned.setTrueName(rs.getString("true_name"));
			vestAssigned.setVestId(rs.getInt("vest_id"));
			vestAssigned.setMsgId(rs.getInt("msg_id"));
			vestAssigned.setStatus(rs.getInt("STATUS"));
			return vestAssigned;
		}
	}
	
	private static final class IntMapper implements RowMapper<Integer> {
		public Integer mapRow(ResultSet rs, int rowNum) throws SQLException {
			int ret = rs.getInt(1);
			return ret;
		}
	}

	public int saveVestAssigned(VestAssigned vestAssigned)
			throws DataAccessException {
		CustomerContextHolder.setCustomerType(CustomerContextHolder.DATA_SOURCE_WRITE);
		String sql="INSERT INTO vestassigned (vest_id,true_id,fake_id,true_name,fake_name,TEXT,STATUS,msg_id) VALUES (?,?,?,?,?,?,?,?)";
		Object[]params = new Object[]{vestAssigned.getVestId(),vestAssigned.getTrueId(),vestAssigned.getFakeId(),vestAssigned.getTrueName(),
				vestAssigned.getFakeName(),vestAssigned.getTest(),vestAssigned.getStatus(),vestAssigned.getMsgId()};
		int ret = jdbcTemplate.save(sql, params);
		CustomerContextHolder.clearCustomerType();
		return ret;
	}

	public int updateVestAssigned(VestAssigned vestAssigned)
			throws DataAccessException {
		CustomerContextHolder.setCustomerType(CustomerContextHolder.DATA_SOURCE_WRITE);
		String sql="UPDATE vestassigned SET vest_id=?,true_id=?,fake_id=?,true_name=?,fake_name=?,TEXT=?,STATUS = ?,msg_id=? WHERE id=?";
		Object[]params = new Object[]{vestAssigned.getVestId(),vestAssigned.getTrueId(),vestAssigned.getFakeId(),vestAssigned.getTrueName(),
				vestAssigned.getFakeName(),vestAssigned.getTest(),vestAssigned.getStatus(),vestAssigned.getMsgId(),vestAssigned.getId()};
		int ret = jdbcTemplate.save(sql, params);
		CustomerContextHolder.clearCustomerType();
		return ret;
	}

	public VestAssigned getvestAssignedById(Integer id)
			throws DataAccessException {
		CustomerContextHolder.setCustomerType(CustomerContextHolder.DATA_SOURCE_READ);
		String sql="SELECT 	id,vest_id,true_id,fake_id,true_name,fake_name,TEXT,STATUS,msg_id FROM vestassigned WHERE vest_id=?";
		VestAssigned vestAssigned = jdbcTemplate.queryForObject(sql, new Object[] {id}, new VestAssignedMapper());
		CustomerContextHolder.clearCustomerType();
		return vestAssigned;
	}

	@Override
	public VestAssigned isOrNotVestAssigned(int trueid, int fakeid, int status)
			throws DataAccessException {
		CustomerContextHolder.setCustomerType(CustomerContextHolder.DATA_SOURCE_READ);
		String sql="SELECT 	id,vest_id,true_id,fake_id,true_name,fake_name,TEXT,STATUS,msg_id FROM vestassigned WHERE true_id=?"
				+ " AND fake_id=? AND STATUS=?";
		VestAssigned vestAssigned = jdbcTemplate.queryForObject(sql, new Object[] {trueid,fakeid,status}, new VestAssignedMapper());
		CustomerContextHolder.clearCustomerType();
		return vestAssigned;
	}

	@Override
	public int getvestAssignedCount(int vestid) throws DataAccessException {
		CustomerContextHolder.setCustomerType(CustomerContextHolder.DATA_SOURCE_READ);
		Object[] obj = null;
		String sql="SELECT count(*) from vestassigned";
		if(vestid!=-1)
		{
			sql = sql+" where vest_id=?";
			obj = new Object[]{vestid};
		}else
		{
			obj = new Object[]{};
		}
		int ret = jdbcTemplate.queryForObject(sql, obj, new IntMapper());
		CustomerContextHolder.clearCustomerType();
		return ret;
	}

	@Override
	public int delVestAssigned(int id) throws DataAccessException {
		// TODO Auto-generated method stub
		CustomerContextHolder.setCustomerType(CustomerContextHolder.DATA_SOURCE_WRITE);
		String sql="DELETE FROM vestassigned  WHERE id=?";
		Object[] params = new Object[]{id};
		int ret = jdbcTemplate.update(sql, params);
		CustomerContextHolder.clearCustomerType();
		return ret;
	}


}
